//
//  RootViewController.swift
//  个人微博
//
//  Created by guo3920591 on 16/4/15.
//  Copyright © 2016年 guo3920591. All rights reserved.
//

import UIKit

import HMRefresh

class RootViewController: UIViewController {
    //公共属性

    
    /// 表格视图
     var tableView: UITableView?
    /// 刷新控件
    lazy var refreshControl = HMRefreshControl()

    /// 访客视图 -如果用户登录成功，就不需要创建访客视图
    //如果使用懒加载会在登陆成功仍创建访客视图，造成无谓的内存开销
    var vistorView: VisitorView?
   /// 视图生命周期
    
    override func loadView() {
        //创建跟视图
        
        view = UIView()
        
        /// 根据用户登录状态显示不同的view
     OAuthModel.shareUserAccount.isLogin ? setupTabview() : setuploginView()
        
        

        
     
    }
    

    override func viewDidLoad() {
        super.viewDidLoad()
        
/// 如果没有登录注册通知
        
        if OAuthModel.shareUserAccount.isLogin {
            loadData()
        }else{
            //注册通知
            NSNotificationCenter.defaultCenter().addObserver(self, selector: "loginSucess", name: GXLWeiboLoginSuccessNofication, object: nil)
        }
        
        

    }
 /// 加载数据-----真正的数据加载应该放在子类
    func loadData(){
        
        
        
    }
    
/// 登录成功监听方法
    @objc private func loginSucess(){
        //一设置view为nil就会调用loadvIEW
        view = nil
        
        
        NSNotificationCenter.defaultCenter().removeObserver(self, name: GXLWeiboLoginSuccessNofication, object: nil)
        
        
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

}

extension RootViewController:VisitorViewDelegate{
    
    func visitorViewDidLogin() {
        
      let OAuthVc = OAuthViewController()
       
      let nav = UINavigationController(rootViewController: OAuthVc)
        
        presentViewController(nav, animated: true, completion: nil)
        
    }
    
    func visitorViewDidRegister() {
        
        print("点击注册")
    }
    
}

extension RootViewController{
    /// 设置访客视图
    private func setuploginView(){
        
        vistorView = VisitorView()
        
        view.addSubview(vistorView!)
        //自动布局
        vistorView?.snp_makeConstraints(closure: { (make) -> Void in
            
            make.edges.equalTo(view)
        })
        
        
        
        vistorView?.delegate = self
        
      navigationItem.rightBarButtonItem = UIBarButtonItem(title: "登陆", style: .Plain, target: self, action: "visitorViewDidLogin")
        navigationItem.leftBarButtonItem = UIBarButtonItem(title: "注册", style: .Plain, target: self, action: "visitorViewDidRegister")
        
        
    }
    
    
    
/// 设置tabview
    private func setupTabview(){
        
        tableView = UITableView(frame: CGRectZero, style: .Plain)
        
        view.addSubview(tableView!)
        
        tableView?.snp_makeConstraints(closure: { (make) -> Void in
            
            make.edges.equalTo(view)
        })
        
        tableView?.dataSource = self
        
        tableView?.delegate = self
    }
    /// 设置刷星控件
    //需要则调用
    func setupControl(){
        
        //如果tableview不存在直接返回
        guard let tableView = tableView else {
            
            return
        }
        
        tableView.addSubview(refreshControl)
        

       //设置下拉刷新视图
        
        refreshControl.pulldownView = GXLPullDownRefreshView.pulldownRefreshView()
        
       //设置刷新控件的监听方法
        
        refreshControl.addTarget(self, action: "loadData", forControlEvents: .ValueChanged)
        
    }
    
}


extension RootViewController:UITableViewDataSource,UITableViewDelegate {
    
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 0
    }
    
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        
        return UITableViewCell()
    }
    
    
    
    
}
